package org.acghub.mtdb.common;

import org.acghub.mtdb.common.type.impl.TypeInt;
import org.acghub.mtdb.common.type.impl.TypeLong;
import org.acghub.mtdb.common.type.impl.TypeString;
import org.w3c.dom.Element;

/**
 * @author zhouhe
 */
public abstract class Type extends Naming implements TypeDependency, VisitAcceptor {

    // 反射调用
    public Type(Naming parent, Element self) throws Exception {
        super(parent, self);
    }

    // 基础类型
    protected Type(String name) {
        super(Root.DEFAULT_ROOT, name);
        this.setResolved(true);
    }

    // 集合类型
    // disable find
    protected Type(Naming parent) {
        super(parent, "");
    }


    public boolean isConstType() {
        return depends().stream().filter(t -> t != this).allMatch(Type::isConstType);
    }

    /**
     * 是否支持表主键
     *
     * @return
     */
    public boolean supportTableKey() {
        return this instanceof TypeInt
                || this instanceof TypeLong
                || this instanceof TypeString;
    }

    public boolean supportTableValue() {
        return this instanceof Bean || this.isConstType();
    }

    public boolean supportAutoIncrement() {
        return false;
    }

    public void supportDefaultValue(String value) {
        throw new UnsupportedOperationException(getName() + "类型不支持默认值:" + value);
    }

    /**
     * 获取java接口类型
     *
     * @return
     */
    public abstract String javaInterfaceType();

    /**
     * 获取java实现类型
     *
     * @return
     */
    public abstract String javaImplType();

    /**
     * 获取默认值
     *
     * @param value
     * @return
     */
    public String getDefaultValue(String value) {
        throw new UnsupportedOperationException();
    }
}
